from flask import Flask
from setting.default import map_config


# 负责创建app
def create_app(config_type):
    app = Flask(__name__)
    # 加载项目的配置
    app.config.from_object(map_config.get(config_type))

    # 初始化限制器
    from comment.utils.limiter import limiter as lmt
    lmt.init_app(app)

    # 加载日志的配置
    from comment.utils.logging import create_logger
    create_logger(app)

    # 初始化SQLAlchemy
    from comment.models import db
    db.init_app(app)

    # 初始化redis
    from comment.utils.shopping_redis import redis_client
    redis_client.init_app(app)

    # 添加请求钩子
    from comment.utils.requests_wares import jwt_request_authorization
    app.before_request(jwt_request_authorization)   # 所有的服务器请求都会进入当前请求钩子

    # 加载模块的蓝图
    from shopping.resources.user import user_bp
    app.register_blueprint(user_bp)

    from shopping.resources.index import index_bp
    app.register_blueprint(index_bp) # 在app中注册蓝图

    from shopping.resources.goods import goods_bp
    app.register_blueprint(goods_bp)


    return app

    # app = Flask(__name__)
    # # 加载项目配置
    # app.config.from_object(map_config.get(config_type))
    #
    # # 加载日志处理的工具
    # from comment.utils.logging import create_logger
    # create_logger(app)
    #
    # # 初始化sqlalchemy
    # from comment.models import db
    # db.init_app(app)
    #
    # # 初始化redis数据库连接
    # from comment.utils.shopping_redis import redis_client
    # redis_client.init_app(app)
    #
    # # 加载模块的蓝图
    # from shopping.resources.user import user_bp
    # app.register_blueprint(user_bp)
    #
    # return app
